package com.itboye.pondteam.pondteam;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.TimePicker;

import com.itboye.pondteam.R;
import com.itboye.pondteam.base.BaseActivity;
import com.itboye.pondteam.bean.PersonDataBean;
import com.itboye.pondteam.custom.ClearEditText;
import com.itboye.pondteam.logincontroller.LoginController;
import com.itboye.pondteam.logincontroller.LoginedState;
import com.itboye.pondteam.popwindow.CustomTimePickerDialog;
import com.itboye.pondteam.presenter.UserPresenter;
import com.itboye.pondteam.utils.Const;
import com.itboye.pondteam.utils.SPUtils;
import com.itboye.pondteam.utils.loadingutil.MAlert;
import com.itboye.pondteam.utils.loadingutil.UIAlertView;
import com.itboye.pondteam.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;


/**
 * Created by itboye on 2017/2/24.
 */

public class LoginActivity extends BaseActivity implements Observer {
    private TextView btn_login, btn_register;//denglu
    private UIAlertView dialog;
    String email, password;
    ClearEditText editextUsetName, editextPassword;
    String userName = "";
    String userPass = "";
    UserPresenter userPresenter;
    TextView txt_forget_pass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        userPresenter = new UserPresenter(this);
    }


    @Override
    public void onClick(View view) {
        userName = editextUsetName.getText().toString();
        userPass = editextPassword.getText().toString();
        Intent intent = null;
        switch (view.getId()) {
            case R.id.btn_register:
                intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
//                onUIAlertView(view);
                break;
            case R.id.btn_login:
                if (userName.equals("") || userName.equals("")) {
                    MAlert.alert(getString(R.string.username_empty));
                    return;
                }
                showProgressDialog(getString(R.string.logining), false);
                userPresenter.login(userName, userPass);
//                intent = new Intent(LoginActivity.this, AddPondDevice.class);
//                startActivity(intent);
//                onShowDlog();
                break;
            case R.id.txt_forget_pass:
                intent = new Intent(LoginActivity.this, ForgetPasswordActivity.class);
                startActivity(intent);
                break;
        }
    }


    /***
     * 时间选择器
     */
    public void onTime() {
        Dialog mPicker = new CustomTimePickerDialog(LoginActivity.this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                String debugStr = "hourOfDay=" + hourOfDay + ",minute= " + minute;
                Log.d("TEMP", debugStr);
//                        mPeriod.period1.hourOn = hourOfDay;
//                        mPeriod.period1.minOn = minute / 10 ;
//                        showPeriod(mPeriod);
//                        Log.d("TEMP",mPeriod.toString());
            }
        }, 1, 2 * 10, true, "时段1开启时间");
        mPicker.show();
    }


    public void onShowDlog() {
        AlertDialog mAlertDialog = new AlertDialog.Builder(
                LoginActivity.this)
                .setTitle("请选择鱼缸水温")
                .setPositiveButton(getString(R.string.cancel), null)
                .setNegativeButton(getString(R.string.ok),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which) {

                            }
                        }).create();
        mAlertDialog.show();
    }

    @Override
    public void update(Observable o, Object data) {
        try {
            closeProgressDialog();
        }catch (Exception e){

        }
        ResultEntity resultEntity = handlerError(data);
        if (resultEntity != null) {
            if (resultEntity.getCode() != 0) {
                MAlert.alert(resultEntity.getMsg());
                return;
            }
            if (resultEntity.getEventType() == UserPresenter.login_success) {
                PersonDataBean personDataBean = (PersonDataBean) resultEntity.getData();
                if (personDataBean != null) {
                    setMyData(personDataBean);
                    MAlert.alert(getString(R.string.login_success));
                    Intent intent = new Intent(this, MyDeviceActivity.class);
                    startActivity(intent);
                    finish();
                }
            } else if (resultEntity.getEventType() == UserPresenter.login_fail) {
                MAlert.alert(resultEntity.getData());
            }

        }
    }

    public void setMyData(PersonDataBean bean) {
        SPUtils.put(LoginActivity.this, null,
                Const.UID, bean.getId());
        SPUtils.put(LoginActivity.this, null,
                Const.PaySecret, bean.getPaySecret());
        SPUtils.put(LoginActivity.this, null,
                Const.RELE, "6");

        SPUtils.put(LoginActivity.this, null,
                Const.USERNAME, bean.getUsername());
        SPUtils.put(LoginActivity.this, null,
                Const.PASSWORD, bean.getPassword());
        SPUtils.put(LoginActivity.this, null,
                Const.MOBILE, bean.getMobile());
        SPUtils.put(LoginActivity.this, null,
                Const.IS_LOGINED, true);
        SPUtils.put(LoginActivity.this, null,
                Const.HEAD, bean.getHead());
        SPUtils.put(LoginActivity.this, null,
                Const.NICK, bean.getNickname());
//        SPUtils.put(LoginActivity.this, null,
//                Const.ISAUTH, bean.getRolesInfo().get(0).getIsAuth());
        SPUtils.put(LoginActivity.this, null,
                Const.S_ID, bean.getAutoLoginCode());

        LoginController.setLoginState(new LoginedState());
    }
}
